//
//  Misc.swift
//  QHEntry-Exit
//
//  Created by Groot on 2017/4/26.
//  Copyright © 2017年 Groot. All rights reserved.
//

import MBProgressHUD

// Date
public func DateCounter(fDateStr: String, type: NSCalendar.Unit) -> Int {
    let dateformatter = DateFormatter()
    dateformatter.dateFormat = "yyyyMMdd"
    let dateresult = dateformatter.date(from: fDateStr)
    
    let gregorian = NSCalendar(calendarIdentifier: .gregorian)
    var result = gregorian?.components(type, from: Date(), to:dateresult!, options: NSCalendar.Options.init(rawValue: 0))
    print(result?.day ?? 0)
    print(result?.month ?? 0)
    print(result?.year ?? 0)
    if type == NSCalendar.Unit.day {
        return result?.day ?? 0
    }else if type == NSCalendar.Unit.month {
        return result?.month ?? 0
    }else if type == NSCalendar.Unit.year {
        return result?.year ?? 0
    }
    
    return 0
}

// Toaster
//public func toastShort(_ str: String) {
//    Toast.init(text: str, delay: 0, duration: 2.5).show()
//}
//public func toastLong(_ str: String) {
//    Toast.init(text: str, delay: 0, duration: 4).show()
//}
// ProgressHUD
public func ShowProgressHUD(_ view: UIView, _ text: String = "") -> MBProgressHUD {
    let hud = getNormalHUD(view)
    hud.mode = .indeterminate
    hud.label.text = text
    hud.show(animated: true)
    return hud
}

public func getNormalHUD(_ view: UIView) -> MBProgressHUD{
    let hud = MBProgressHUD.showAdded(to: view, animated: true)
    hud.bezelView.color = UIColor.init(white: 0, alpha: 0.8)
    hud.contentColor = UIColor.white
    hud.label.font = UIFont.systemFont(ofSize: 14)
    return hud
}
//public func MsgTips(title: String, content: String) {
//    let messageView: MessageView = MessageView.viewFromNib(layout: .CenteredView)
//    //messageView.configureBackgroundView(width: 250)
//    messageView.configureContent(title: title, body: content, iconImage: nil, iconText: nil, buttonImage: nil, buttonTitle: "关闭") { _ in
//        SwiftMessages.hide()
//    }
//    messageView.button?.isHidden = true
//    messageView.configureTheme(backgroundColor: UIColor.clear, foregroundColor: .white)
//    var config = SwiftMessages.defaultConfig
//    config.presentationStyle = .center
//    config.duration = .forever
//    config.dimMode = .blur(style: .dark, alpha: 0.9, interactive: true)
//    config.presentationContext  = .window(windowLevel: UIWindowLevelStatusBar)
//    SwiftMessages.show(config: config, view: messageView)
//}
//public func MsgAlert(_ title: String?, _ body: String?) -> MessageView {
//
//    let view: MessageView =  MessageView.viewFromNib(layout: .CardView)
//    view.configureContent(title: title!, body:body!, iconText: "!")
//    view.iconImageView?.isHidden = true
//    view.iconLabel?.isHidden = true
//
//    view.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14.0)
//    view.bodyLabel?.font = UIFont.systemFont(ofSize: 13.0)
//    view.configureDropShadow()
//    //view.button?.setImage(Icon.errorSubtle.image, for: .normal)
//    view.button?.setTitle("查看", for: .normal)
//    view.button?.backgroundColor = UIColor.clear
//    //view.button?.tintColor = UIColor.green.withAlphaComponent(0.7)
//    var config = SwiftMessages.defaultConfig
//    config.duration = .seconds(seconds: 5)
//    SwiftMessages.show(config: config, view: view)
//
//    return view;
//}

public func hudToastShort(view: UIView, text: String) {
    let hud = getNormalHUD(view)
    hud.mode = .text
    hud.label.text = text
    hud.hide(animated: true, afterDelay: 2.0)
}

public func hudToastLong(view: UIView, text: String) {
    let hud = getNormalHUD(view)
    hud.mode = .text
    hud.label.text = text
    
    hud.hide(animated: true, afterDelay: 4.0)
}

// String
extension String {
    
    func transformToPinYin() -> String {
        
        let mutableString = NSMutableString(string: self)
        //把汉字转为拼音
        CFStringTransform(mutableString, nil, kCFStringTransformToLatin, false)
        //去掉拼音的音标
        CFStringTransform(mutableString, nil, kCFStringTransformStripDiacritics, false)
        
        let string = String(mutableString)
        //去掉空格
        return string.replacingOccurrences(of: " ", with: "").uppercased()
    }
}

// Image

public func thumbnailWithImage(img: UIImage?, size: CGSize) -> UIImage {
    var newImg: UIImage = UIImage.init()
    if let image = img {
        let oldsize = image.size
        var rect = CGRect.init()
        if size.width / size.height > oldsize.width / oldsize.height {
            rect.size.width = size.height * oldsize.width / oldsize.height
            rect.size.height = size.height
            rect.origin.x = (size.width - rect.size.width) / 2
            rect.origin.y = 0
        }else {
            rect.size.width = size.width
            rect.size.height = size.width * oldsize.height / oldsize.width
            rect.origin.x = 0
            rect.origin.y = (size.height - rect.size.height) / 2
        }
        
        UIGraphicsBeginImageContext(size)
        //        let context: CGContext = UIGraphicsGetCurrentContext()!
        //        context.setFillColor(UIColor.white as! CGColor)
        UIRectFill(CGRect.init(x: 0, y: 0, width: size.width, height: size.height))
        image.draw(in: rect)
        newImg = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
    }
    return newImg
}


